package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntTaskCheckMapper;
import com.prd.model.EntTaskCheck;
import com.prd.model.LoginUser;
import com.prd.service.EntTaskCheckService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntTaskCheckServiceImpl extends ServiceImpl<EntTaskCheckMapper, EntTaskCheck> implements EntTaskCheckService {

    @Autowired
    private EntTaskCheckMapper entTaskCheckMapper;

    @Override
    public int insertInfo(EntTaskCheck entTaskCheck) {
        LambdaQueryWrapper<EntTaskCheck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntTaskCheck::getTaskId, entTaskCheck.getTaskId());
        queryWrapper.eq(EntTaskCheck::getType, entTaskCheck.getType());
        EntTaskCheck TaskCheck = entTaskCheckMapper.selectOne(queryWrapper);
        if (TaskCheck != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entTaskCheck);
        return entTaskCheckMapper.insert(entTaskCheck);
    }

    @Override
    public int updateInfo(EntTaskCheck entTaskCheck) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entTaskCheck);
        return entTaskCheckMapper.updateById(entTaskCheck);
    }
}
